/**
 * 
 */
package ro.dta.idbi.patterns;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ro.dta.idbi.interfaces.IntegrationOperation;
import ro.dta.idbi.model.AbstractPattern;
import ro.dta.idbi.model.AbstractPrimitivePattern;
import uk.ac.ic.doc.automed.IntegrityException;
import uk.ac.ic.doc.automed.NotFoundException;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;

/**
 * <b>Reverse transformation for the column to subtype transformation
 * pattern</b>
 * 
 * Example of use:<br />
 * 
 * <code>subtype_to_col (&lt;&lt;childTable,col&gt;&gt;, &lt;&lt;parentTable&gt;&gt;)</code>
 * <br />
 * 
 * @author Tudor Dobrila
 * 
 */
public class ColSubtypeRev extends AbstractPattern {

	/**
	 * Name of the pattern
	 */
	public static final String NAME = "Subtype to Column";

	/**
	 * Description of the pattern
	 */
	public static final String DESCRIPTION = "Remove subtype";

	/**
	 * Name of the command associated with the pattern
	 */
	public static final String COMMAND = "subtype_to_col";

	/**
	 * Integration phase during which the command is applied
	 */
	public static final EnumSet<IntegrationOperation> OPERATION = EnumSet
			.of(IntegrationOperation.CONFORMING);

	/**
	 * Name of the original column
	 */
	private String origColStr;

	/**
	 * Name of the child table
	 */
	private String newTableStr;

	/**
	 * Subtype to column pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Source schema over which the transformation is applied
	 */
	public ColSubtypeRev(final Schema sourceSchema) throws NotFoundException, IntegrityException {
		super(sourceSchema);

		pattern = Pattern.compile("^" + COMMAND + "[ ]*\\((" + COL_REGEX + ")" + COMMA_REGEX + "("
				+ TABLE_REGEX + ")\\)$");
	}

	/**
	 * Subtype to column pattern class constructor
	 * 
	 * @param sourceSchema
	 *            Name of the source schema over which the transformation is
	 *            applied
	 */
	public ColSubtypeRev(final String sourceSchema) throws NotFoundException, IntegrityException {
		this(Schema.getSchema(sourceSchema));
	}
	
	@Override
	protected List<String> getPrimitiveCommands() throws NotFoundException, IntegrityException {
		SchemaObject colObj = sourceSchema.getSchemaObject(origColStr);
		Object[] colDef = colObj.getSchemeDefinition();
		
		// Convert to the forward representation of the transformation
		Entry<String, String> col = parseColName(origColStr);
		origColStr = "<<" + newTableStr + "," + col.getValue() + ">>";
		newTableStr = col.getKey();
		
		ColSubtype forward = new ColSubtype(sourceSchema, origColStr, newTableStr, colDef);
		List<String> commands = forward.getPrimitiveCommands();
		List<String> result = new ArrayList<String>();
		for (String cmd : commands) {
			result.add(0, AbstractPrimitivePattern.getReverseTrans(cmd));
		}
		
		return result;
	}

	@Override
	protected boolean verify() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected void parse(String trans) {
		Matcher matcher = pattern.matcher(trans);
		matcher.find();
		origColStr = matcher.group(1);
		newTableStr = parseTableName(matcher.group(2));
	}

}
